package com.piggy.logi.sys.controller;

import com.piggy.common.core.domain.R;
import com.piggy.common.core.utils.poi.ExcelUtil;
import com.piggy.common.core.web.controller.BaseController;
import com.piggy.common.core.web.page.TableDataInfo;
import com.piggy.common.log.annotation.LogiLog;
import com.piggy.common.log.enums.BusinessType;
import com.piggy.common.satoken.utils.SecurityUtils;
import com.piggy.logi.sys.bo.LogiGoodSourceEditBo;
import com.piggy.logi.sys.bo.LogiGoodSourceQueryBo;
import com.piggy.logi.sys.service.ILogiGoodSourceService;
import com.piggy.logi.sys.vo.LogiGoodSourceExtVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@Api(value = "货源信息控制器", tags = {"货源信息管理"})
@RestController
@RequestMapping("/logi/goodSource")
public class LogiGoodSourceController extends BaseController {

    @Resource
    private ILogiGoodSourceService iLogiGoodSourceService;

    /**
     * 查询货源信息列表
     */
    @ApiOperation("分页查询货源信息列表")
    //@SaCheckPermission("system:source:list")
    @PostMapping("/list")
    public TableDataInfo<LogiGoodSourceExtVo> list(@Validated @RequestBody LogiGoodSourceQueryBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        return iLogiGoodSourceService.queryPageList(bo);
    }

    /**
     * 导出货源信息列表
     */
    @ApiOperation("导出货源信息列表")
    //@SaCheckPermission("system:source:export")
    @LogiLog(title = "货源信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated @RequestBody LogiGoodSourceQueryBo bo, HttpServletResponse response) throws IOException {
        List<LogiGoodSourceExtVo> list = iLogiGoodSourceService.queryList(bo);
        ExcelUtil<LogiGoodSourceExtVo> util = new ExcelUtil<>(LogiGoodSourceExtVo.class);
        util.exportExcel(response, list, "货源信息");
    }

    /**
     * 获取货源信息详细信息
     */
    @ApiOperation("获取货源信息详细信息")
    //@SaCheckPermission("system:source:query")
    @GetMapping("/{id}")
    public R<LogiGoodSourceExtVo> getInfo(@NotNull(message = "主键不能为空")
                                          @PathVariable("id") Long id) {
        return R.ok(iLogiGoodSourceService.queryById(id));
    }

    /**
     * 新增货源信息
     */
    @ApiOperation("新增货源信息")
    //@SaCheckPermission("system:source:add")
    @LogiLog(title = "货源信息", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Integer> add(@Validated @RequestBody LogiGoodSourceEditBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        bo.setMainDeptId(SecurityUtils.getMainDeptId());
        return R.ok(iLogiGoodSourceService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改货源信息
     */
    @ApiOperation("修改货源信息")
    //@SaCheckPermission("system:source:edit")
    @LogiLog(title = "货源信息", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Integer> edit(@Validated @RequestBody LogiGoodSourceEditBo bo) {
        bo.setDeptId(SecurityUtils.getDeptId());
        bo.setMainDeptId(SecurityUtils.getMainDeptId());
        return R.ok(iLogiGoodSourceService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除货源信息
     */
    @ApiOperation("删除货源信息")
    //@SaCheckPermission("system:source:remove")
    @LogiLog(title = "货源信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Integer> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return R.ok(iLogiGoodSourceService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
